package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liliudong.template.biz.service.sys.SysUserService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.constant.ExMessageEnum;
import com.liliudong.template.common.dto.sys.SysUserAddOrUpdateDTO;
import com.liliudong.template.common.dto.sys.SysUserLoginDTO;
import com.liliudong.template.common.exception.BizException;
import com.liliudong.template.common.vo.PageVO;
import com.liliudong.template.common.vo.sys.SysUserBaseVO;
import com.liliudong.template.common.vo.sys.SysUserLoginSuccessVO;
import com.liliudong.template.common.vo.sys.SysUserPageVO;
import com.liliudong.template.common.vo.sys.SysUserProfileVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户
 *
 * @author ldli5
 * @classname SysUserController
 * @date 2024/6/17-16:56
 */
@RestController
@RequestMapping("/v1/sys/user")
@RequiredArgsConstructor
public class SysUserController {

	private final SysUserService sysUserService;

	/**
	 * 登录
	 *
	 * @param sysUserLoginDTO sys用户登录dto
	 * @return {@link R }<{@link ? }>
	 */
	@PostMapping("/login")
	public R<SysUserLoginSuccessVO> login(@RequestBody @Validated SysUserLoginDTO sysUserLoginDTO) {
		SysUserLoginSuccessVO tokenInfo = sysUserService.login(sysUserLoginDTO.getUsername(), sysUserLoginDTO.getPassword());
		return R.ok(tokenInfo);
	}

	/**
	 * 注销
	 *
	 * @return {@link R }<{@link ? }>
	 */
	@GetMapping("/logout")
	public R<?> logout() {
		StpUtil.logout();
		return R.ok();
	}

	/**
	 * 轮廓
	 *
	 * @return {@link R }<{@link SysUserProfileVO }>
	 */
	@GetMapping("/profile")
	public R<SysUserProfileVO> profile() {
		SysUserProfileVO sysUserProfileVO = sysUserService.profile();
		return R.ok(sysUserProfileVO);
	}

	/**
	 * 添加用户
	 *
	 * @param sysUserAddOrUpdateDTO sys用户添加dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:user:add")
	@PostMapping
	public R<?> addOne(@RequestBody @Validated SysUserAddOrUpdateDTO sysUserAddOrUpdateDTO) {
		sysUserService.addOne(sysUserAddOrUpdateDTO);
		return R.ok();
	}

	/**
	 * 按ID删除
	 *
	 * @param id 身份证件
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:user:delete")
	@DeleteMapping("/{id}")
	public R<?> delete(@PathVariable Long id) {
		if (id == 1L) {
			throw new BizException(ExMessageEnum.CANNOT_DELETE_SUPER_ADMIN);
		}
		sysUserService.removeById(id);
		return R.ok();
	}

	/**
	 * 按ID修改
	 *
	 * @param id                    身份证件
	 * @param sysUserAddOrUpdateDTO sys用户添加或更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:user:modify")
	@PutMapping("/{id}")
	public R<?> modifyById(@PathVariable Long id, @RequestBody SysUserAddOrUpdateDTO sysUserAddOrUpdateDTO) {
		sysUserService.modifyById(id, sysUserAddOrUpdateDTO);
		return R.ok();
	}

	/**
	 * 分页查询
	 *
	 * @param current 现在
	 * @param size    大小
	 * @return {@link R }<{@link IPage }<{@link SysUserPageVO }>>
	 */
	@SaCheckPermission("sys:user:list")
	@GetMapping("/page")
	public R<PageVO<SysUserPageVO>> page(@RequestParam(defaultValue = "1") Long current,
	                                     @RequestParam(defaultValue = "10") Long size) {
		PageVO<SysUserPageVO> page = sysUserService.queryPage(current, size);
		return R.ok(page);
	}

	/**
	 * 按id获取
	 *
	 * @param id 身份证件
	 * @return {@link R }<{@link SysUserBaseVO }>
	 */
	@SaCheckPermission("sys:user:detail")
	@GetMapping("/detail/{id}")
	public R<SysUserBaseVO> detailById(@PathVariable Long id) {
		SysUserBaseVO sysUserBaseVO = sysUserService.detailById(id);
		return R.ok(sysUserBaseVO);
	}
}
